<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>

<body>
    <script>
        function promiseAjax() {
            var promise = new Promise((resolve, reject) => {
                $.ajax({
                    type: 'get',
                    url: 'http://localhost:3000/api/getmsg',
                    success: function(data) {
                        resolve(data);
                    },
                    error: function(err) {
                        reject(err);
                    }
                });
            });
            return promise;
        };
        // promise的结果可以使用,then来进行接受
        // 1.统一api管理异步请求的结果
        // 2.使异步执行的顺序得到控制
        // 3.解决回调地狱
        promiseAjax().then((data) => {
            console.log(data);
        }, (err) => {
            console.log(err);
        });
    </script>
</body>

</html>